package com.doit.day04

/**
 * 增删改查
 */
object ArrayDemo_算子 {
  def main(args: Array[String]): Unit = {

    val arr: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    //需求1：我想要将数组中的所有的元素都*10，用一个新的数组去接受它  ==> map  做映射
    val res: Array[Int] = arr.map(elem => elem * 10)
    println(res.mkString(","))

    //需求2：我只要arr里面的偶数 ==》filter
    val res2: Array[Int] = arr.filter(elem => elem % 2 == 0)
    println(res2.mkString(","))

    //foreach==》没有返回值的  这个算子一般用于打印
    arr.foreach(elem=>println(elem))

    //函数的至简原则
    val res1: Array[Int] = arr.map((elem:Int)=>{elem * 10})
    //如果函数的参数只有一个，那么小括号可以省略，如果函数体里面只有一句话，那么大括号也可以省略
    //类型也可以省略，因为arr的数据类型已经有了，可以自动推断
    val res3: Array[Int] = arr.map(elem=>elem * elem)
    //如果说，这个参数在参数列表中出现一次，并且在函数体里面也只出现了一次，那么可以用_来代替
    val res4: Array[Int] = arr.map(_*10)


    val sum: Int = arr.sum
    val max: Int = arr.max
    val min: Int = arr.min
    //差集
    val ints: Array[Int] = arr.diff(res2)// 1 3 5 7 9
    println(ints.mkString(","))
    //交集
    val ints1: Array[Int] = arr.intersect(res2)  //2  4  6  8 10
    println(ints1.mkString(","))
    //并集
    val ints2: Array[Int] = arr.union(res2)//1-10  2 4 6 8 10
    println(ints2.mkString(","))

    println("====================")

    //拉链
    // 1 2 3 4 5 6 7 8 9 10
    // 2 4 6 8 10
    val tuples: Array[(Int, Int)] = arr.zip(res2)
    tuples.foreach(println)

    val index: Array[(Int, Int)] = arr.zipWithIndex
    index.foreach(println)



    //聚合类型 ==》 原来数组中有很多个元素==》 只剩下一个结果   sum
    //将元素从左往右一次3个为1组，三个为1组
    val iterator: Iterator[Array[Int]] = arr.grouped(3)
    iterator.foreach(elem=>{
      println(elem.mkString(","))
    })


    //groupby 应用非常广泛  ==》   map   filter
    val strings: Array[String] = Array("wang", "hua","bao")
   //统计每个单词出现的次数
   val map: Map[String, Array[String]] = strings.groupBy(word => word)
    map.map(tp=>(tp._1,tp._2.length)).foreach(println)


    //flatten 压平   字符串本身就是一个字符组成的数组   数组中嵌套的数组来进行压平
    val flatten: Array[Char] = strings.flatten
    flatten.foreach(println)

    val ars: Array[Array[String]] = Array(Array("hello","hi"),Array("hive","hbase"),Array("bao","hua"))
    val flatten1: Array[String] = ars.flatten
    flatten1.foreach(println)//Array("hello","hi","hive","hbase","bao","hua")

    //flatMap  ==》 flatten 和 map的结合   先做map，然后在flatten

    val abc: Array[String] = Array("hello,hi,hello", "bao,bao,hua", "bao,hello,hi")
    val arrayAndWords: Array[Array[String]] = abc.map(_.split(","))
    val flatten2: Array[String] = arrayAndWords.flatten//Array(hello,hi,hello,bao,bao。。。。)

    val strings1: Array[String] = abc.flatMap(_.split(","))



   /* val map: Map[String, Array[String]] = strings.groupBy(word => word)
    map.map(elem=>{
      val key: String = elem._1
      val value: String = elem._2.mkString("_")
      (key,value)
    }).foreach(println)


    val tuples1: Array[(Int, String)] = Array(
      (1, "zss"),
      (1, "lss"),
      (2, "zss"),
      (2, "lss"),
      (3, "zss"),
      (4, "ww"))
    //1,Array(zss,lss)

    tuples1.map(tp=>100).foreach(println)*/


/*    val map1: Map[Int, Array[(Int, String)]] = tuples1.groupBy(tp => tp._1)
    val res10: Map[Int, Array[String]] = map1.map(tp => {
      val key: Int = tp._1
      val value: Array[(Int, String)] = tp._2
      val strings1: Array[String] = value.map(tp => tp._2)
      (key, strings1)
    })*/




  }

}
